package com.xingdian.xingdianhulianserver.web;

import com.xingdian.xingdianhulianserver.common.QueryObject;
import com.xingdian.xingdianhulianserver.common.ServerResponse;
import com.xingdian.xingdianhulianserver.service.IpInfoService;
import com.xingdian.xingdianhulianserver.util.ossUtil.UploadUtil;
import com.xingdian.xingdianhulianserver.vo.IpInfo;
import com.xingdian.xingdianhulianserver.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/* *
 * @Author H2SO4
 * @ClassName: IpInfoController
 * @Description TODO
 * @Date 2019/11/26--18:10
 * */
@Controller
public class IpInfoController {

    @Autowired
    IpInfoService ipInfoService;

    //上传图片
    @PostMapping("upload")
    public String uolpadImage(Model model,MultipartFile img){
        String uploadImageKey = UploadUtil.upload(img);
        model.addAttribute("uploadImageKey",uploadImageKey);
        return "add";
    }

    //修改图片
    @PostMapping("toupload")
    public String uolpadImage(MultipartFile img, Integer id1, RedirectAttributes redirectAttributes){
        String uploadImageKey = UploadUtil.upload(img);
        IpInfo ipInfo = new IpInfo();
        ipInfo.setId(id1);
        ipInfo.setImgUrl(uploadImageKey);
        ipInfoService.updateIpInfo(ipInfo);
        //传递参数
        //redirectAttributes.addFlashAttribute("id",id1);
        return "redirect:/updateInfo?id="+id1;
    }

    //保存数据
    @PostMapping("add")
    public String add(Model model, IpInfo ipInfo){
        int count = ipInfoService.add(ipInfo);
        if(count>0){
            return "home";
        }
        return "add";
    }

    //查询需要修改的信息   并跳转到修改页面
    @PostMapping("/update")
    public String update(Model model,IpInfo ipInfo){
        ipInfoService.updateIpInfo(ipInfo);
        return "home";
    }

    //查询需要修改的信息   并跳转到修改页面
    @GetMapping("/updateInfo")
    public String toupdateInfo(Model model,Integer id){
        IpInfo ipInfo = ipInfoService.findById(id);
        model.addAttribute("ipInfo",ipInfo);
        return "update";
    }

    //跳转添加页面
    @GetMapping("toadd")
    public String toAdd(){
        return "add";
    }

    //跳转页面
    @GetMapping("toindex")
    public String toindex(){
        return "index";
    }

    //跳转页面
    @GetMapping("tomain")
    public String tomain(){
        return "main";
    }

    //跳转页面
    @GetMapping("tohome")
    public String tohome(){
        return "home";
    }

    //跳转页面
    @GetMapping("tonav")
    public String tonav(){
        return "nav";
    }

    //跳转页面
    @GetMapping("tonews")
    public String tonews(){
        return "news";
    }

    //查询数据
    @PostMapping("findinfo")
    @ResponseBody
    public ServerResponse findinfo(QueryObject queryObject){
        return ipInfoService.findinfo(queryObject);
    }

    //删除
    @GetMapping("delInfo")
    public String delInfo(Integer id){
        ipInfoService.delInfo(id);
        return "home";
    }

    //登录
    @PostMapping("login")
    @ResponseBody
    public Integer login(String userName, String password, HttpSession session){
        User user = ipInfoService.login(userName,password);
        if(user!=null){

            session.setAttribute("token",(int)(Math.random()*10)*2*3*5*7);
            session.setMaxInactiveInterval(10800);
            return 1;
        }
        return 2;
    }

/*    //删除图片
    @PostMapping("deleteImg")
    @ResponseBody
    public ServerResponse deleteImg(String url){
        return UploadUtil.deleteImg(url);
    }*/

}
